package com.martin.dms.web.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;

public class CategoryManagementBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -1886068940164660313L;

    private List<Long> categoryIds = new ArrayList<Long>();

    public void setCategoryIds(List<Long> categoryIds) {
        this.categoryIds = categoryIds;
    }

    public List<Long> getCategoryIds() {
        return categoryIds;
    }

    public void validateCategoriesManagement(ValidationContext validationContext) {
        MessageContext messageContext = validationContext.getMessageContext();
        String userEvent = validationContext.getUserEvent();
        if (userEvent.equals("delete") || userEvent.equals("edit")) {
            if (categoryIds == null || categoryIds.size() != 1) {
                messageContext.addMessage(new MessageBuilder().error().code("error.selectOneCategory").build());
            }
        }
    }
}
